package vista;

import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VentanaProducto extends javax.swing.JFrame {

    private controlador.ControladorProducto control;
    private DefaultTableModel modeloTabla;
    private Vector<String> fila;

   
    public VentanaProducto() {
        initComponents();
        control= new controlador.ControladorProducto(this);
        addAction();
        modeloTabla= new DefaultTableModel();
        fila= new Vector<String>();
        tblIngrediente.setModel(modeloTabla);
        fila.add("Ingrediente");
        fila.add("Cantidad");
        modeloTabla.setColumnIdentifiers(fila);
        addAction();
    }

     public boolean actualizarCabtidad(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()))){
               modeloTabla.setValueAt(spnCantidad.getValue(), i, 1);
               return true;
            }
        return false;
    }


    public void agregarFila(String [] v){
        if(!actualizarCabtidad()){
                fila= new Vector<String>();
                fila.add(v[0]);
                fila.add(v[1]);
                modeloTabla.addRow(fila);
            }
    }

     public void agregarFila1(String [] v){
        fila= new Vector<String>();
        fila.add(v[0]);
        fila.add(v[1]);
        modeloTabla.addRow(fila);
    }

    public void menos(){
        int i = tblIngrediente.getSelectedRow();
        if(i != -1)
            modeloTabla.removeRow(i);
        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar el regitstro que desea quitar","Calle Hambre Arena Plaza",JOptionPane.ERROR_MESSAGE);
    }

    

    public void addListener(ActionListener escuchador){
        btnSalir.addActionListener(escuchador);
        btnRegistrar.addActionListener(escuchador);
        btnCancelar.addActionListener(escuchador);
        btnMas.addActionListener(escuchador);
        btnMenos.addActionListener(escuchador);
    }
    
    
    public void addAction(){
      btnSalir.setActionCommand("Salir");
        btnMas.setActionCommand("Mas");
        btnCancelar.setActionCommand("Cancelar");
        btnMenos.setActionCommand("Menos");
        btnRegistrar.setActionCommand("Registrar");

}

    public void cargarComboIngrediente(LinkedList<String> descripciones){
        cmbIngredientes.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbIngredientes.addItem(descripciones.get(i));

        }
    }

    public void cargarComboCategoria(LinkedList<String> descripciones){
        cmbCategoria.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbCategoria.addItem(descripciones.get(i));

        }
    }

    public JTable getTblIngrediente() {
        return tblIngrediente;
    }

     public void limpiar(){
        txtCodigo.setText("");
        txtDescripcion.setText("");
        txtPrecio.setText("");
        cmbCategoria.setSelectedItem(0);
        cmbIngredientes.setSelectedItem(0);
        spnCantidad.setValue(0);
    }
    


     public LinkedList<JTextField> getTextos() {
        LinkedList<JTextField> v = new LinkedList<JTextField>();
        return v;
    }

    public String [] getRegistro(){
        String [] s = new String [] {""+cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()),""+getCantidadIngrediente()};

         return s;
    }

    public Integer getCantidadIngrediente() {
        return (Integer) spnCantidad.getValue();
    }

    public boolean hayVacios(){
        for (int i = 0; i < getTextos().size(); i++) {
            JTextField texto = getTextos().get(i);
            if(texto.getText().equals("")){
                JOptionPane.showMessageDialog(new JFrame(),"Campos vacios","Cari Burguer",JOptionPane.INFORMATION_MESSAGE);
                return false;
            }
        }
        return true;
    }

   public boolean validarCantidad(){
        if(getCantidadIngrediente()<=0){
                JOptionPane.showMessageDialog(new JFrame(),"Cantidad no permitida","Cari Burguer",JOptionPane.INFORMATION_MESSAGE);
                return false;
            }

        return true;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtDescripcion() {
        return txtDescripcion;
    }

    public JTextField getTxtPrecio() {
        return txtPrecio;
    }
   

    public boolean validoNumeros(){
    try{
           if(Double.parseDouble(getTxtPrecio().getText())<=0){
                getTxtPrecio().setText("");
                JOptionPane.showMessageDialog(new JFrame(),"El precio no es valido","Cari Burguer",JOptionPane.WARNING_MESSAGE);
                return false;
           }
       }catch(NumberFormatException ex){
           JOptionPane.showMessageDialog(new JFrame(),"Debe ingresar valores numericos","Cari Burguer",JOptionPane.WARNING_MESSAGE);
           return false;
       }
    return true;
    }

    public String getTextoCmbCategoria() {
        return (String )cmbCategoria.getItemAt(cmbCategoria.getSelectedIndex());
    }

    public String getTextoCmbIngredientes() {
        return (String )cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex());
    }

    public LinkedList<String []> getInfoTabla(){
        LinkedList<String []> v = new LinkedList<String[]>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++){
            String [] s = {""+modeloTabla.getValueAt(i, 0),""+modeloTabla.getValueAt(i, 1)};
            v.add(s);
         }
         return v;
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblIngrediente = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblIngrediente1 = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cmbCategoria = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        cmbIngredientes = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        spnCantidad = new javax.swing.JSpinner();
        btnMas = new javax.swing.JButton();
        btnMenos = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registro de Productos");
        getContentPane().setLayout(null);

        tblIngrediente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblIngrediente);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(13, 251, 401, 129);
        jScrollPane1.setBackground(new java.awt.Color(255,255,255));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(null);

        tblIngrediente1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblIngrediente1);

        jPanel1.add(jScrollPane2);
        jScrollPane2.setBounds(20, 470, 650, 160);

        jLabel11.setFont(new java.awt.Font("Ubuntu", 1, 15));
        jLabel11.setText("Ingredientes");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(420, 270, 110, 30);

        jLabel6.setFont(new java.awt.Font("Ubuntu",1,15));
        jLabel6.setText("Codigo");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(23, 37, 96, 24);

        jLabel1.setFont(new java.awt.Font("Ubuntu",1,15));
        jLabel1.setText("Categoria");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(23, 8, 113, 22);

        jPanel1.add(cmbCategoria);
        cmbCategoria.setBounds(148, 9, 224, 22);
        cmbCategoria.setBackground(new java.awt.Color(255,255,255));
        cmbCategoria.setSize(224, 20);

        jLabel2.setFont(new java.awt.Font("Ubuntu",1,15));
        jLabel2.setText("Descripcion");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(25, 67, 118, 23);

        jLabel3.setFont(new java.awt.Font("Ubuntu",1,15));
        jLabel3.setText("Precio");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(25, 96, 86, 22);
        {
        	txtPrecio = new javax.swing.JTextField();
        	jPanel1.add(txtPrecio);
        	txtPrecio.setFont(new java.awt.Font("Arial", 0, 13));
        	txtPrecio.setBounds(148, 97, 224, 24);
        	txtPrecio.setSize(224, 20);
        }
        {
        	txtDescripcion = new javax.swing.JTextField();
        	jPanel1.add(txtDescripcion);
        	txtDescripcion.setFont(new java.awt.Font("Arial", 0, 13));
        	txtDescripcion.setBounds(148, 68, 224, 24);
        	txtDescripcion.setSize(224, 20);
        }
        {
        	txtCodigo = new javax.swing.JTextField();
        	jPanel1.add(txtCodigo);
        	txtCodigo.setBounds(148, 39, 224, 22);
        	txtCodigo.setSize(224, 20);
        }
        
        jPanel1.add(txtPrecio);

        jPanel1.add(txtDescripcion);
        jPanel1.add(txtCodigo);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(12, 28, 402, 125);
        jPanel1.setBackground(new java.awt.Color(255,255,255));

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setLayout(null);

        jPanel2.add(cmbIngredientes);
        cmbIngredientes.setBounds(123, 7, 170, 23);
        cmbIngredientes.setBackground(new java.awt.Color(255,255,255));

        jLabel5.setFont(new java.awt.Font("Ubuntu",1,15));
        jLabel5.setText("Ingrediente");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(21, 8, 95, 23);

        jLabel7.setFont(new java.awt.Font("Ubuntu",1,15));
        jLabel7.setText("Porcion");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(22, 35, 84, 21);
        jPanel2.add(spnCantidad);
        spnCantidad.setBounds(124, 35, 36, 23);

        btnMas.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnMas.setText("Agregar");
        jPanel2.add(btnMas);
        btnMas.setBounds(305, 7, 84, 30);
        btnMas.setSize(84, 23);

        btnMenos.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnMenos.setText("Quitar");
        jPanel2.add(btnMenos);
        btnMenos.setBounds(304, 37, 87, 23);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(12, 177, 402, 68);
        jPanel2.setBackground(new java.awt.Color(255,255,255));

        jLabel4.setFont(new java.awt.Font("Ubuntu",1,15));
        jLabel4.setText("Registro de Productos");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(128, 2, 186, 22);
        jLabel4.setBackground(new java.awt.Color(255,255,255));

        jLabel8.setFont(new java.awt.Font("Ubuntu",1,15));
        jLabel8.setText("Ingredientes");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(146, 155, 109, 22);
        {
        	btnRegistrar = new javax.swing.JButton();
        	getContentPane().add(btnRegistrar);
        	btnRegistrar.setFont(new java.awt.Font("Ubuntu",1,15));
        	btnRegistrar.setText("Registrar");
        	btnRegistrar.setBounds(19, 392, 100, 23);
        }
        {
        	btnCancelar = new javax.swing.JButton();
        	getContentPane().add(btnCancelar);
        	btnCancelar.setFont(new java.awt.Font("Ubuntu",1,15));
        	btnCancelar.setText("Cancelar");
        	btnCancelar.setBounds(163, 393, 100, 23);
        }
        {
        	btnSalir = new javax.swing.JButton();
        	getContentPane().add(btnSalir);
        	btnSalir.setFont(new java.awt.Font("Ubuntu",1,15));
        	btnSalir.setText("Salir");
        	btnSalir.setBounds(305, 392, 100, 23);
        }
        
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds(0, 0, 429, 457);
        getContentPane().setBackground(new java.awt.Color(255,255,255));
    }// </editor-fold>//GEN-END:initComponents

   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaProducto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnMas;
    private javax.swing.JButton btnMenos;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbCategoria;
    private javax.swing.JComboBox cmbIngredientes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner spnCantidad;
    private javax.swing.JTable tblIngrediente;
    private javax.swing.JTable tblIngrediente1;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtPrecio;
    // End of variables declaration//GEN-END:variables



}
